<?php
/**
 *file-level documentation
 *@author Name <email@exp.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, Tuncay Bakan
 *@package somePackage
 *
 */
/**
 *shows the home site
 */
class PriceRequest_Controller extends Controller{
    /**
     * Holds id of page to be displayed
     * @var integer
     */
    private $iPid = '';

    /**
     * Language in which to show page
     * @var integer
     */
    private $iLang = '';
    /**
     * Stores the method which was called
     * @var string
     */
    private $sMethod;
    /**
     * True if the request was sent
     * @var boolean
     */
    private $bRequestSent = 0;
    /**
     * Determines the level from which to get the page structure
     * @var mixed
     */
    private $mNavigationRoot = 'NULL';

    /**
     * Reference to view of content section
     * @var object
     */
    private $oContentView;

    /**
     * Reference to model that retrieves page data from db
     * @var object
     */
    private $oContentModel;

    /**
     * Reference to model that retrieves page navigation elements from db
     * @var object
     */
    private $oNavigationModel;

    /**
     * Reference to view of navigation section
     * @var object
     */
    private $oNavigationView;

    /**
     * Reference to view of left navigation section in cms, its shows the entire pagestructure
     * @var object
     */
    private $oLeftNavigationView;
    /**
     * Ensures that the user is able to see where he is located within the homepage
     * @var object
     */
    private $oLocationView;

    public function __construct(){
        //Gets Viewmanger reference from parent constructor
        parent::__construct();
        //Loads models and views
        $this->oContentModel = Loader::loadModel('productPage');
        $this->oNavigationModel = Loader::loadModel('navigation');
        $this->oLocationView = $this->oViewmanager->getView('location');
        $this->oContentView = $this->oViewmanager->getView('priceRequest');
        $this->oLeftNavigationView = $this->oViewmanager->getView('leftnavigation');

        //Checks language set in Cookie, take default language specified in config if not set
        if (isset($_COOKIE['language'])){
            $this->iLang = intval($_COOKIE['language']);
        }else{
            $this->iLang = "1";
        }
    }
/**
 * Checks GET-Parameters forwarded from Router::route
 * @param array arguements to check 
 */
    public function checkArgs($aArgs){
        if (isset($aArgs['id'])&&$aArgs['id']!=''){
            $this->iPid = intval($aArgs['id']);
        }

        if(isset($aArgs['m'])){
            $this->sMethod = $aArgs['m'];
        }

        //Check whether a method was called
        if($this->sMethod == 'sendRequest'){
            if (isset($this->iPid)){
                $this->sendRequest();
                $this->bRequestSent = 1;
                $this->oContentView->assign('sendStatus', $this->bRequestSent);
            }
        }
        if($this->bRequestSent == 0){
            $this->oContentView->assign('sendStatus', $this->bRequestSent);

        }
    }

    public function execute($aArgs){
        $this->checkArgs($aArgs);

        $this->oLocationView->assign('location', $this->oNavigationModel->getLocation($this->iPid, $this->iLang, 'product'));
        $this->oNavigationView = $this->oViewmanager->getView($GLOBALS['NAVIGATIONTEMP']);
        $this->oContentView->assign('language', $this->iLang);
        $this->oContentView->assign('product', $this->oContentModel->getProductDetail($this->iPid,$this->iLang));
        //Checks if mobile device is used
        if($GLOBALS['ISMOBILE']==1){
            $this->oNavigationView->assign('navigation', $this->oNavigationModel->getPageStructure($this->mNavigationRoot,$this->iLang));
        }else{
            $this->oNavigationView->assign('navigation', $this->oNavigationModel->getPageStructure('NULL',$this->iLang));
        }
        $this->oLeftNavigationView->assign('productsANDcategories', $this->oNavigationModel->getProdCat('',$this->iLang, 'product'));
//Inserts data in the correct sections of the website layout
        $this->oViewmanager->setSection('Location', $this->oLocationView->display());
        $this->oViewmanager->setSection('Content', $this->oContentView->display());
        $this->oViewmanager->setSection('LeftNavigationTitle', $this->oNavigationModel->getLeftNavigationTitle($this->iLang));
        $this->oViewmanager->setSection('LeftNavigation', $this->oLeftNavigationView->display());
        $this->oViewmanager->setSection('Navigation', $this->oNavigationView->display());
    }
/**
 * Ajax function to load the price request template
 */
    public function getViewContent(){
        $this->checkArgs($_GET);
        $this->oContentView->assign('language', $this->iLang);
        $this->oContentView->assign('product', $this->oContentModel->getProductDetail($this->iPid,$this->iLang));
        echo $this->oContentView->display();
        //exit();
    }
    /**
     * Sends the price request
     */
    public function sendRequest(){
        $sRecipient = DEFAULT_EMAIL;
        $sMailtext= implode("", file("./formularTemplates/priceRequest.htt"));
        foreach($_POST as $key=> $val) {
            $sMailtext = str_replace("<$key>", $val, $sMailtext);
        }
        if ($_POST["access"] == "irregeheim") {
            mail($sRecipient, $_POST["subject"], $sMailtext);
        }

    }
}
